<?php

namespace App\Admin\Controllers;

use App\Models\Users;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Show;

class SysUsersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Users());
        $grid->column('', __('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'));
        $grid->column('id', __('ID'));
        $grid->column('avatar', __('头像'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('nick_name', __('微信昵称'));
        $grid->column('name', __('姓名'));
        $grid->column('parent.name', __('邀请人姓名'));
        $grid->column('parent.phone', __('邀请人手机号'));
        $grid->column('open_id', __('openId'))->help('微信授权标识码');
        $grid->column('phone', __('手机号'));
        $grid->column('id_card', __('身份证'));
        $grid->column('money', __('账户余额'));

        // 设置text、color、和存储值
        $states = [
            'on' => ['value' => 1, 'text' => '启用中', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭中', 'color' => 'danger'],
        ];
        $grid->column('is_qrcode', __('开启/关闭邀请码'))->switch($states);


        $grid->column('created_at', __('授权时间'));

        $grid->column('parent_user_id' , __('用户来源'))->display(function ($parentUserId) {

            if($parentUserId != 0){
                return "<span style='color:green'>用户推广</span>";
            }else{
                return "<span style='color:red'>自主注册</span>";
            }
        });

        $grid->expandFilter();

        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->like('phone', __('手机号'));

                $filter->like('id_card', __('身份证'));

                $filter->between('created_at', __('注册时间'))->datetime();
            });

            $filter->column(1 / 2, function ($filter) {
                $filter->like('parent.name', __('邀请人姓名'));

                $filter->like('parent.phone', __('邀请人手机号'));

                $filter->where(function ($query) {
                    switch ($this->input) {
                        case 1:
                            // custom complex query if the 'yes' option is selected
                            $query->where('parent_user_id' , '!=' , 0);
                            break;
                        case 2:
                            $query->where('parent_user_id' , '=' , 0);
                            break;
                    }
                }, '用户来源')->radio([
                    '' => '全部',
                    1 => '用户推广',
                    2 => '自主注册',
                ]);
            });

        });
        //禁用创建按钮
        $grid->disableCreateButton();

        // 全部关闭
        $grid->actions(function ($actions) {

            // 去掉编辑
            $actions->disableEdit();

            $actions->disableDelete();

            // 去掉查看
            $actions->disableView();
        });
        // 去掉批量操作
        $grid->disableBatchActions();

        $grid->disableExport();

        $grid->setActionClass(Actions::class);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Users::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('parent_user_id', __('Parent user id'));
        $show->field('name', __('Name'));
        $show->field('nick_name', __('Nick name'));
        $show->field('avatar', __('Avatar'));
        $show->field('open_id', __('Open id'));
        $show->field('phone', __('Phone'));
        $show->field('money', __('Money'));
        $show->field('original', __('Original'));
        $show->field('code', __('Code'));
        $show->field('is_qrcode', __('Is qrcode'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Users());

//        $form->number('parent_user_id', __('Parent user id'));
//        $form->text('name', __('Name'));
//        $form->text('nick_name', __('Nick name'));
//        $form->image('avatar', __('Avatar'));
//        $form->text('open_id', __('Open id'));
//        $form->mobile('phone', __('Phone'));
//        $form->decimal('money', __('Money'))->default(0.00);
//        $form->textarea('original', __('Original'));
//        $form->text('code', __('Code'));

        $states = [
            'on' => ['value' => 1, 'text' => '启用中', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭中', 'color' => 'danger'],
        ];
        $form->switch('is_qrcode', __('开启/关闭邀请码'))->states($states);

        $form->tools(function (Form\Tools $tools) {

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();

        });

        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        return $form;
    }
}
